#!/bin/bash
#项目名
CI_PROJECT_NAME=$1
#分支名
CI_BUILD_REF_NAME=$2
#用户名
USER=$3
#最新commit值
CI_COMMIT_SHA=$4
#打包路径
PACK_PATH=$5
#下包地址
DOWNLOAD_BASE_URL=$6

pwd

echo "start build...."

rm -rf output/
rm -rf output.tar.gz
mkdir output

npm config get registry
yarn config get registry
npm -v
node -v
ifconfig
pwd

npm install --force




echo "当前环境：---- ${CI_BUILD_REF_NAME}"

if [ "${CI_BUILD_REF_NAME}" = "master" ]
then
  #线上执行命令
    npm run lib:Europe
elif [ "${CI_BUILD_REF_NAME}" = "Europe" ] # 线上-欧洲
then
    npm run lib:Europe
elif [ "${CI_BUILD_REF_NAME}" = "sea" ] # 线上-东南亚
then
    npm run lib:sea
elif [ "${CI_BUILD_REF_NAME}" = "testEurope" ] # 测试-欧洲
then
    npm run test:Europe
elif [ "${CI_BUILD_REF_NAME}" = "testSea" ] # 测试-东南亚
then
    npm run test:sea
elif [ "${CI_BUILD_REF_NAME}" = "dev" ] # 测试-内网-欧洲
then
    npm run lib:dev
elif [ "${CI_BUILD_REF_NAME}" = "seaDev" ] # 测试-内网-东南亚
then
    npm run lib:seaDev
else
  #其他环境执行命令
    npm run lib:dev
fi

cp -r ossFile output/


mv dist output



echo "all done"

echo "---------BEGIN 压缩代码包 BEGIN---------"

if [ ! -d  $PACK_PATH ]
then
  echo "打包目录不存在，待创建"
  mkdir -p $PACK_PATH
  echo "打包目录创建完毕"
else
  echo "打包目录已存在。"
fi

pwd
echo "开始替换版本文件.."
rm -rf $PACK_PATH/version.txt && touch $PACK_PATH/version.txt
echo ${CI_COMMIT_SHA:0:8}.${USER}.tar.gz >> $PACK_PATH/version.txt
echo "替换版本文件完毕!"

echo "发起任务用户: ${USER} 提交版本: ${CI_COMMIT_SHA}"
echo "开始打包..."
echo "CI_COMMIT_SHA: ${CI_COMMIT_SHA} 提交版本: ${PACK_PATH}"
tar -zcf $PACK_PATH/${CI_COMMIT_SHA:0:8}.${USER}.tar.gz output/

tarMD5=`md5sum $PACK_PATH/${CI_COMMIT_SHA:0:8}.${USER}.tar.gz | awk '{print $1}'`
echo $tarMD5 >> $PACK_PATH/version.txt


echo "打包完成!"
echo "-----------END 压缩代码包 END-----------"
